package com.java.control.impl;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;
import org.springframework.validation.BindException;

import com.java.common.SessionInfo;
import com.java.control.AbstractAdminFormController;
import com.java.control.search.CodeSearchCond;
import com.java.control.search.UserSearchCond;
import com.java.entity.Code;
import com.java.entity.FactoryPayment;
import com.java.entity.OrderForm;
import com.java.entity.User;
import com.java.mis.UserType;
import com.java.service.CodeService;
import com.java.service.OrderService;
import com.java.service.UserService;

/**
 * 产品添加
 * @author wqf
 * @version  Nov 23, 2011 1:10:42 PM
 */
public class OrderEditController extends AbstractAdminFormController{
	
	public OrderEditController(){
		this.setCommandClass(OrderForm.class);
	}
		
	protected void addSubmit(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors) throws Exception {
		OrderForm order = (OrderForm)command;
		try{
			this.orderService.editOrder(order);
		} catch (Exception e) {
			return;
		}
		request.setAttribute("operation", "add");
	}	
	
	protected void modifySubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception{
		
		OrderForm order = (OrderForm)command;
		order.setStatus((order.getStatus()!=null&&order.getStatus().getId()==null)?null:order.getStatus());
		order.setCustomerGrade(order.getCustomerGrade()!=null&&order.getCustomerGrade().getId()==null?null:order.getCustomerGrade());
		this.orderService.editOrder(order);
		
		request.setAttribute("operation", "modify");
		request.setAttribute("id", order.getId());
	}
		
	protected Object getCommandDataForModify(HttpServletRequest request){
		Long id=Long.valueOf(request.getParameter("id"));
		if(null!=id){
			OrderForm order = this.orderService.getOrderById(id,false);
			if(order!=null){ 
				if(order.getStatus()==null){
					order.setStatus(new Code());
				}			
				if(order.getCustomerGrade()==null){
					order.setCustomerGrade(new Code());
				}
				if(order.getUser()==null){
					SessionInfo session = getSessionInfo(request);
					if(session!=null){
						if(session.getUserType()==UserType.select){
							User user = this.userService.getUserById(getSessionInfo(request).getUserId());
							order.setUser(user);
						}else{
							order.setUser(new User());
						}			
					}
				}		
			}
			return order;
		}
		return null;
	}
	
	protected Object getCommandDataForAdd(HttpServletRequest request) {
		OrderForm order = new OrderForm();
		order.setStatus(new Code());
		order.setCustomerGrade(new Code());
		SessionInfo session = getSessionInfo(request);
		if(session!=null){
			if(session.getUserType()==UserType.select){
				User user = this.userService.getUserById(getSessionInfo(request).getUserId());
				order.setUser(user);
			}else{
				order.setUser(new User());
			}			
		}
		return order;
	}

	@SuppressWarnings("unchecked")
	protected Map referenceData(HttpServletRequest request) throws Exception {
		ModelMap map = new ModelMap();
		
		CodeSearchCond codeSearchCond = new CodeSearchCond();
		codeSearchCond.setCodeType("订单状态");
		List<Code> statusList = this.codeService.search(Code.class,codeSearchCond);
		map.addObject("statusList", statusList);
		
		codeSearchCond = new CodeSearchCond();
		codeSearchCond.setCodeType("客户等级");
		List<Code> gradeList = this.codeService.search(Code.class,codeSearchCond);
		map.addObject("gradeList", gradeList);
		
		//用户信息
		UserSearchCond userSearchCond = new UserSearchCond();
//		userSearchCond.setIsAdmin(false);//允许管理添加订单
		userSearchCond.setStatus(true);
		List<User> userList = this.userService.search(User.class,userSearchCond);
		map.addObject("userList", userList);
		
		return map;
	}
		
	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public OrderService getOrderService() {
		return orderService;
	}
	private UserService userService;
	public OrderService orderService;
	public CodeService codeService;
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}
